/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			lib_main.cc
  \date			Jul 2011
  \author		TNick

  \brief		Contains library's entry point

*//*

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<main.h>

#include    <QDebug>
#include    <QApplication>
#include    <QDesktopWidget>
#include    <QDir>

#include    <env.h>


/**
*	@brief	 internal flags
*/
enum		AppFlags	{
	NO_FLAG = 0

};


/**
*	@brief	 operation modes
*/
enum		AppOpMode	{
	APP_HELP_MODE = 0,
	APP_COMPILE_MODE,
	APP_RUN_MODE
};

/**
*	@brief	 the extension for files
*/
#define	FILES_EXTENSION		"lll"




/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/**
*	@brief	 list of files to load at start-up
*/
QStringList					start_suite_;

/**
*	@brief	 list of arguments
*/
QMap<QString, QString>		arg_lst_;

/**
*	@brief	 application flags
*/
AppFlags					flg_;

/**
*	@brief	 application opperation mode
*/
AppOpMode					op_mode_;


/**
*	@brief	 maximum size of source files in bytes
*/
int							max_src_size_;


/*  DATA    ================================================================ */
//
//
//
//
/*  FUNCTIONS    ----------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
bool		filesInDirP			(
		QString s_path, QString pattern, QStringList & out_files )
{
	QDir dir( s_path );
	if ( dir.exists() == false )
		return false;

	QStringList filters ( pattern );
	QFileInfoList list = dir.entryInfoList( filters );
	for ( int i = 0; i < list.size(); ++i )
	{
		QFileInfo fInfo = list.at(i);
		out_files.append( fInfo.absoluteFilePath() );
	}
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool		doFile				( QString s_path )
{

	Env * env = Env::loadFile( s_path );
	if ( env == NULL )
	{
		return false;
	}


	return true;

}
/* ========================================================================= */



/* ------------------------------------------------------------------------- */
int		lib_main				( int argc, char *argv[] )
{


	try
	{
		SETTINGS_Init();

		//Q_INIT_RESOURCE( visualqt );
		op_mode_ = APP_RUN_MODE;
		max_src_size_ = 1 * 1024 * 1024;

		/* prepare to start */
		QApplication apl( argc, argv );
		Q_UNUSED( apl );

		/* prepare the ground for easy access to settings */
		QCoreApplication::setOrganizationName( "TNick" );
		QCoreApplication::setOrganizationDomain( "sites.google.com/site/tnickweb/" );
		QCoreApplication::setApplicationName( APP_NAME );


		/* tokenize list of arguments and check it */
		QStringList arg_str_lst = QCoreApplication::arguments( );



		int i = 1;
		bool b_msg = false;
		while ( i < arg_str_lst.count( ))	{
			const QString arg_up = arg_str_lst.at( i ).toUpper( );
			if ( arg_up.startsWith( "-" ))
			{
				if ( arg_up == "-HELP" )
				{
					op_mode_ = APP_HELP_MODE;

					/**
					  @todo present help message
					 */

					return 0;
				}
				else if ( arg_up == "-COMPILE" )
				{
					op_mode_ = APP_COMPILE_MODE;
				}
				else if ( arg_up == "-RUN" )
				{
					op_mode_ = APP_RUN_MODE;
				}

				//				else if ( arg_up == "-HELP" )
				//				{
				//					i++;

				//					/* next argument is the file name */
				//					if ( i == arg_str_lst.count( ))
				//					{ /* needs argument */
				//						u_msg.add(
				//									UserMsg::MSG_ERROR,
				//									QObject::tr( "Argument <-BATCH> needs a script file to work" )
				//									);
				//						b_msg = true;
				//					}
				//					else
				//					{
				//						arg_lst_.insert( QString( "BATCH" ), arg_str_lst[i] );
				//					}

				//					flg_ = static_cast<App::AppFlags>( flg_ | BATCH_MODE );

				//				}
				else
				{
					ErrorReport(
								QObject::tr( "Argument <%1> was not understood" )
								.arg( arg_str_lst.at( i ))
								);
					b_msg = true;
				}
			}
			else
			{ /* file that should be opened */
				start_suite_ << arg_str_lst.at( i );
			}
			i++;
		} /* while ( i < arg_str_lst.count( )) */


		/* if there were errors, exit */
		if ( b_msg )
		{
			return -1;
		}

		/* if there is no file input, use current folder */
		QString crt_p = QDir::current().path();
		if ( start_suite_.count( ) == 0 )
		{
			filesInDirP( crt_p,
						 "*." FILES_EXTENSION,
						 start_suite_ );
			if ( start_suite_.count() == 0 )
			{
				ErrorReport(
							QObject::tr( "No data file found in <%1> folder" )
							.arg( crt_p )
							);
				return -1;
			}
		}

		/* iterate in files or folders */
		QFile	f_tst_;
		for ( int i = 0; i < start_suite_.count( ); i++ )
		{
			f_tst_.setFileName( start_suite_.at( i ) );
			if ( f_tst_.exists() )
			{
				doFile( start_suite_.at( i ) );
			}
			else
			{
				int crt_cnt = start_suite_.count();
				QStringList sl_files;
				if ( filesInDirP( start_suite_.at( i ), "*." FILES_EXTENSION, sl_files ) )
				{
					if ( crt_cnt == start_suite_.count() )
					{
						WarningReport(
									QString("Folder <%1> does not contain any data files!")
									.arg( start_suite_.at( i ) )
									);
					}
					else
					{
						start_suite_.append( sl_files );
					}
				}
				else
				{
					WarningReport(
								QString("Folder/File <%1> does not exists!")
								.arg( start_suite_.at( i ) )
								);
				}
			}
		}


		//QDir::setCurrent(QCoreApplication::applicationDirPath());

		return 0;

	}
	catch (...) {  }
	return -1;
}
/* ========================================================================= */






/*  FUNCTIONS    =========================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
